package com.example.zzm.yuejian03.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Bean.BaominBean;
import com.example.zzm.yuejian03.Bean.TrainBean;
import com.example.zzm.yuejian03.Bean.collectBean;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Model.BaominModel;
import com.example.zzm.yuejian03.Model.TrainModeldetails;
import com.example.zzm.yuejian03.R;
import com.example.zzm.yuejian03.Server;
import com.squareup.picasso.Picasso;

public class BaominActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView11,textView62,textView64,textView66,textView68,textView70,textView88,textView80;
    private TextView editText5,editText6;
    private Button button29,button30,button25;
    private int unitprice;
    private int userid;
    private SharedPreferences sp;
    private int resid;

    public void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baoming);
        initView();
        initEvent();
        initDate();
        resid  = getIntent().getIntExtra("train_id",1);//获取传递的资源id
        sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        readSP();//读取sessionid
        TrainModeldetails BaominModel = new TrainModeldetails();
        BaominModel.getTraindetailss(getIntent().getIntExtra("train_id",1),listener);

    }
    private void readSP() {
        userid = sp.getInt("user_id",1);
    }
    TListener<TrainBean> listener = new TListener<TrainBean>() {
        @Override
        public void onResponse(TrainBean club) {
            textView62.setText(club.getTrain_name());
            textView64.setText(club.getTime());
            textView66.setText(club.getAddress());
            textView68.setText(Integer.toString(club.getPrice()));
            textView70.setText(club.getIntro());
//            textView80.setText("1");
            unitprice = club.getPrice();
        }
        @Override
        public void onFail(String msg) {

        }
    };
    TListener<BaominBean> tlistener = new TListener<BaominBean>() {
        @Override
        public void onResponse(BaominBean baominBean) {
                editText6.setText(baominBean.getReal_name());
                editText5.setText(baominBean.getPhone());
                textView80.setText(Integer.toString(baominBean.getUnit_price()));
                textView88.setText("1");
                unitprice = baominBean.getUnit_price();
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<collectBean> ttlistener = new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean baominBean) {
            if (baominBean.getError().equals("0")) {
                Toast.makeText(BaominActivity.this, "结算成功", Toast.LENGTH_SHORT).show();
                finish();
            } else
                Toast.makeText(BaominActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };
    private void initDate() {

    }
    public String toAllPrice(int num) {

        int price = unitprice * num;
        String Price = Integer.toString(price);
        return Price;
    }
    private void initEvent() {
        textView11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button29.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(textView88.getText().toString()) == 1) {
                    Toast.makeText(BaominActivity.this, "不能再少了", Toast.LENGTH_SHORT).show();
                } else {
                    textView88.setText(Integer.toString(Integer.parseInt(textView88.getText().toString()) - 1));
                    textView80.setText(toAllPrice(Integer.parseInt(textView88.getText().toString())));
                }
            }
        });
        button30.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView88.setText(Integer.toString(Integer.parseInt(textView88.getText().toString()) + 1));
                textView80.setText(toAllPrice(Integer.parseInt(textView88.getText().toString())));
            }
        });
        button25.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaominModel model=new BaominModel();//实例化对象
                if(button25.getText().equals("报名"))//如果已收藏，则调用取消收藏
                {
                    model.getBaomins(resid,userid,tlistener);
                    button25.setText("结算");
                    Toast.makeText(BaominActivity.this,"报名成功！", Toast.LENGTH_SHORT).show();
                }
                else//如果未收藏，则调用收藏
                {
                    model.getBaominss(resid,userid,Integer.parseInt(textView88.getText().toString()),ttlistener);
                    button25.setText("支付");
                    Toast.makeText(BaominActivity.this,"支付成功！", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    private void initView() {
        textView11 = (TextView)findViewById(R.id.textView11);
        editText5 = (TextView) findViewById(R.id.textView5);
        editText6 = (TextView) findViewById(R.id.textView6);
        textView62 = (TextView)findViewById(R.id.textView62);
        textView64 = (TextView)findViewById(R.id.textView64);
        textView66 = (TextView)findViewById(R.id.textView66);
        textView68 = (TextView)findViewById(R.id.textView68);
        textView70 = (TextView)findViewById(R.id.textView70);
        button29 = (Button)findViewById(R.id.button29);
        button30 = (Button)findViewById(R.id.button30);
        textView88 = (TextView) findViewById(R.id.textView88);
        textView80 = (TextView)findViewById(R.id.textView80);
        button25 =(Button)findViewById(R.id.button25);
    }

    @Override
    public void onClick(View v) {

    }
}
